import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Goods from '../components/goods/goods.vue'
import addGoods from '../components/goods/addGood.vue'
import upadGood from '../components/goods/upadGood.vue'
import User from '../components/user/users.vue'
import Orders from '../components/orders/orders.vue'
import Roles from '../components/roles/roles.vue'
import Rrights from '../components/roles/rights.vue'
import Reports from '../components/reports/reports.vue'
import Categories from '../components/goods/categories.vue'
import Params from '../components/goods/params.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/users',
        name: 'User',
        component: User,
      },
      {
        path: '/goods',
        name: 'Goods',
        component: Goods,
      },
      {
        path: '/orders',
        name: 'Orders',
        component: Orders,
      },
      {
        path: '/roles',
        name: 'Roles',
        component: Roles,
      }, {
        path: '/add',
        name: 'addGoods',
        component: addGoods,
      }, {
        path: '/rights',
        name: 'Rrights',
        component: Rrights,
      }, {
        path: '/reports',
        name: ' Rports',
        component: Reports,
      }, {
        path: '/categories',
        name: ' Categories',
        component: Categories,
      }, {
        path: '/params',
        name: ' Params',
        component: Params,
      }, {
        path: '/updat',
        name: 'upadGood',
        component: upadGood,
      }
    ]
  },
  {
    path: '/',
    name: 'Login',
    component: Login
  }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if(to.name=='Login'){
    next()
  }

  const tokenStr = localStorage.getItem('token')
  // console.log(tokenStr);
  if(!tokenStr){
    next('/')
  }else{
    next()
  }
})


export default router
